﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;

using System.Data.OleDb;
using System.Data.SqlServerCe;

using Microsoft.VisualStudio.TextTemplating;
using Microsoft.VisualStudio.TextTemplating.VSHost;

using System.IO;

namespace fpdToolkit
{
    public partial class Form1 : Form
    {
        List<string> tables;
        /// <summary>
        /// Путь к базе данных
        /// </summary>
        public static string path_to_db { get { return Properties.Settings.Default.fpd_path; } set { Properties.Settings.Default.fpd_path = value; Properties.Settings.Default.Save(); } }
        //public static string path_to_db = @"C:\CKLAD";

        /// <summary>
        /// Строка подключения к БД Visual Foxpro
        /// </summary>
        /// <returns></returns>
        public static string connection_string()
        {
            return "Provider=VFPOLEDB.1;Data Source=" + path_to_db + ";DELETED=True";
        }
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            XmlWriterSettings settings = new XmlWriterSettings() { Indent = true };
            XmlWriter textWriter = XmlWriter.Create("schema.xml", settings);
            tables = new List<string>();
            using (OleDbConnection connection = new OleDbConnection(connection_string()))
            {
                connection.Open();
                DataTable tbl = connection.GetSchema("tables");
                foreach (DataRow row in tbl.Rows)
                {
                    tables.Add(row["TABLE_NAME"].ToString());
                }

                OleDbDataReader myReader;
                textWriter.WriteStartDocument();
                textWriter.WriteStartElement("FPD26");
                foreach (string tablename in tables)
                {
                    textWriter.WriteStartElement("TABLE");
                    textWriter.WriteStartElement("NAME");
                    textWriter.WriteString(tablename);
                    textWriter.WriteEndElement();
                    string query = "SELECT * from " + tablename;
//                    System.Console.WriteLine(tablename);
                    OleDbCommand command = new OleDbCommand(query, connection);
                    myReader = command.ExecuteReader();
                    tbl = myReader.GetSchemaTable();
                    
                        foreach (DataRow myField in tbl.Rows)
                        {
                            //For each property of the field...
                            textWriter.WriteStartElement("FIELD");
                            
                            textWriter.WriteStartElement("ColumnName");
                            textWriter.WriteString(myField["ColumnName"].ToString());
                            textWriter.WriteEndElement();

                            textWriter.WriteStartElement("ColumnSize");
                            textWriter.WriteString(myField["ColumnSize"].ToString());
                            textWriter.WriteEndElement();

                            textWriter.WriteStartElement("NumericPrecision");
                            textWriter.WriteString(myField["NumericPrecision"].ToString());
                            textWriter.WriteEndElement();

                            textWriter.WriteStartElement("NumericScale");
                            textWriter.WriteString(myField["NumericScale"].ToString());
                            textWriter.WriteEndElement();

                            textWriter.WriteStartElement("DataType");
                            textWriter.WriteString(myField["DataType"].ToString());
                            textWriter.WriteEndElement();

                            textWriter.WriteStartElement("ProviderType");
                            textWriter.WriteString(myField["ProviderType"].ToString());
                            textWriter.WriteEndElement();

                            //foreach (DataColumn myProperty in tbl.Columns)
                            //{
                            //    //Display the field name and value.
                            //    Console.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());
                            //}
                            //Console.WriteLine();
                            textWriter.WriteEndElement();
                        }
                        textWriter.WriteEndElement();
                    
                    myReader.Close();
                }
                textWriter.WriteEndElement();
                textWriter.WriteEndDocument();
                textWriter.Close();  
                connection.Close();



            //    OleDbCommand command = new OleDbCommand(queryString, connection);

            //    connection.Open();
            //    OleDbDataReader reader = command.ExecuteReader();

            //    while (reader.Read())
            //    {
            //        Console.WriteLine(reader[0].ToString());
            //    }
            //    reader.Close();
            }

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            FormEditDataSchema frm = new FormEditDataSchema();
            frm.Show();
        }

    }
}


//for (int i = 0; i < myReader.FieldCount; i++)
//{

//    Console.WriteLine("{0}={1}", myReader.GetName(i), myReader[i].ToString());
//}
//Console.WriteLine();

//                DataTable tbl =  myReader.GetSchemaTable();
//                foreach (DataRow myField in tbl.Rows)
//                {
//                    foreach (DataColumn myProperty in tbl.Columns)
//                    {
//                        //Display the field name and value.
//                        Console.WriteLine(myProperty.ColumnName + " = " + myField[myProperty].ToString());
//                    }
//                    Console.WriteLine();
////                    tables.Add(row["TABLE_NAME"].ToString());
//                }